package com.design_pattern._03_behavioral._07_chain;

import java.util.ArrayList;
import java.util.List;

interface Filter {
    boolean doFilter(Canned canned);
}

public class V3 {
    public static void main(String[] args) {
        Canned canned = new Canned();
        canned.status = true;

        System.out.println("制作罐头");
        FilterChain chain = new FilterChain();
        chain.add(new CheckFilter()).add(new ClearFilter()).add(new CutFilter());
        chain.doFilter(canned);

    }
}

class FilterChain {
    List<Filter> filterList = new ArrayList<>();

    public FilterChain add(Filter filter) { // 返回this，可以用链式的方法编写代码，更方便
        filterList.add(filter);
        return this;
    }

    public void doFilter(Canned canned) {
        for (Filter filter : filterList) {
            boolean b = filter.doFilter(canned);
            if (!b) {
                // 本以为打印接口的Class，没想到打印的是内存空间的class，很nice
                System.out.println(filter.getClass().getSimpleName() + "制作过程中发现存在问题，停止制作");
                return;
            }
        }
    }
}

class CheckFilter implements Filter {
    public boolean doFilter(Canned canned) {
        System.out.println("100行代码，进行检查");
        return canned.status;
    }
}

class ClearFilter implements Filter {
    public boolean doFilter(Canned canned) {
        System.out.println("100行代码，进行清洗");
        return canned.clear;
    }
}

class CutFilter implements Filter {
    public boolean doFilter(Canned canned) {
        System.out.println("100行代码，进行切片");
        return true;
    }
}